home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgzip_022-src / macos / Posix / ThinkCPosix Sources / dup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  522 b   |  25 lines  |  [TEXT/MPS ]

  1. /* $Id: $ */
  2.  
  3. /*
  4.  * This implementation of dup() disables fd;
  5.  * in effect, it assumes that fd is closed
  6.  * immediately after dup() (as is almost always the case).
  7.  * The problem is that if __file[fd]->func() is left unchanged
  8.  * then close(fd) will also close dup(fd).
  9.  */
  10.  
  11. #include "ThinkCPosix.h"
  12.  
  13. int dup(int fd)
  14. {
  15.     FILE *fp1 = fdopen(fd, "");
  16.     FILE *fp2;
  17.     
  18.     if (fp1 == NULL)
  19.         return -1;
  20.     fp2 = __getfile();
  21.     memcpy((char*)fp2, (char*)fp1, sizeof(FILE));
  22.     memset((char*)fp1, 0, sizeof(FILE));
  23.     return fileno(fp2);
  24. }
  25.